<template>
  <div class="big">
    <h1 style="text-align: center" class="bt">注册</h1>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="rules.username"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="rules.password"
      />
      <van-field
        v-model="repass"
        type="password"
        name="repass"
        label="确认密码"
        placeholder="确认密码"
        :rules="rules.repass"
      />
      <van-field
        v-model="email"
        name="email"
        label="邮箱"
        placeholder="邮箱"
        :rules="rules.email"
      />
      <van-field
        v-model="tel"
        name="tel"
        label="手机号"
        placeholder="手机号"
        :rules="rules.tel"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
        color="#ff5100">提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import Vue from "vue";
import { Form, Field, Button ,Toast} from "vant";
import { Register } from "@/request/request";
Vue.use(Form);
Vue.use(Field);
Vue.use(Button);
Vue.use(Toast);

export default {
  data() {
    return {
      rules: {
        username: [{ required: true, message: "请填写用户名" }],
        password: [{ required: true, message: "请填写密码" }],
        repass: [{ required: true, message: "请再次填写密码" }],
        email: [
          {
            pattern:
              /^([1-9]\d{4,9}@qq\.com|[a-zA-Z]\w{5,17}@((126|163)\.com|yeah\.net))$/,
            message: "请输入正确的邮箱",
          },
        ],
        tel: [{ pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号" }],
      },
      username: "",
      password: "",
      repass: "",
      tel: "",
      email: "",
    };
  },
  methods: {
    onSubmit(values) {
      delete values.repass
      Register(values).then((res)=>{
        Toast({
          message:'注册成功！',
          onClose:()=>{
            this.$router.back()
          }
        });
      })
    },
  },
};
</script>

<style lang="scss" scoped>
.big{
  padding: 50px;
  .bt{
    margin-bottom: 10px;
  }
}
</style>
